package bb.lanxing.lib.devices.sprint.utils;

import static android.bluetooth.BluetoothDevice.ACTION_BOND_STATE_CHANGED;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import com.umeng.analytics.pro.d;
//import bb.lanxing.lib.kt.XossCoroutineScopeKt;
import kotlin.Metadata;
import kotlin.Unit;
import kotlin.jvm.internal.Intrinsics;
import kotlin.text.StringsKt;

public final class DeviceBondInspector {
    private BondListener bondListener;
    private final BroadcastReceiver broadcastReceiver;
    private final Context context;

    public interface BondListener {
        void onBondFailed();

        void onBondSuccess();

        void onBonding();
    }

    public DeviceBondInspector(Context context, final String address) {
        Intrinsics.checkNotNullParameter(context, "context");
        Intrinsics.checkNotNullParameter(address, "address");
        this.context = context;
        this.broadcastReceiver = new BroadcastReceiver() {
            @SuppressLint("MissingPermission")
            @Override
            public void onReceive(Context context2, Intent intent) {
                if (intent != null) {
//                    if (!StringsKt.equals$default(intent.getAction(), ACTION_BOND_STATE_CHANGED, false, 2, null)) {
//                        intent = null;
//                    }
                    if (intent == null) {
                        return;
                    }
                    String str = address;
                    BroadcastReceiver deviceBondInspector = this;
                    BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    if (bluetoothDevice == null) {
                        return;
                    }
                    if (!Intrinsics.areEqual(bluetoothDevice.getAddress(), str)) {
                        bluetoothDevice = null;
                    }
                    if (bluetoothDevice == null) {
                        return;
                    }
                    //TODO:hu
                    switch (bluetoothDevice.getBondState()) {
                        case 10:
//                            XossCoroutineScopeKt.runOnMainThread(new DeviceBondInspector$broadcastReceiver$1$onReceive$2$2$1(deviceBondInspector, null));
                            Log.d("DeviceBondListener", "已解除配对");
                            return;
                        case 11:
//                            XossCoroutineScopeKt.runOnMainThread(new DeviceBondInspector$broadcastReceiver$1$onReceive$2$2$2(deviceBondInspector, null));
                            Log.d("DeviceBondListener", "正在配对...");
                            return;
                        case 12:
//                            XossCoroutineScopeKt.runOnMainThread(new DeviceBondInspector$broadcastReceiver$1$onReceive$2$2$3(deviceBondInspector, null));
                            Log.d("DeviceBondListener", "已配对");
                            return;
                        default:
                            return;
                    }
                }
            }
        };
    }

    public final Context getContext() {
        return this.context;
    }

    public final BondListener getBondListener() {
        return this.bondListener;
    }

    public final void setBondListener(BondListener bondListener) {
        this.bondListener = bondListener;
    }

    public final BroadcastReceiver getBroadcastReceiver() {
        return this.broadcastReceiver;
    }

    public final void inspect(BondListener bondListener) {
        Intrinsics.checkNotNullParameter(bondListener, "bondListener");
        this.bondListener = bondListener;
        Context context = this.context;
        BroadcastReceiver broadcastReceiver = this.broadcastReceiver;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_BOND_STATE_CHANGED);
        context.registerReceiver(broadcastReceiver, intentFilter);
    }

    public final void reset() {
        try {
            this.context.unregisterReceiver(this.broadcastReceiver);
            this.bondListener = null;
        } catch (Exception unused) {
        }
    }
}
